|
|
|
|
|
|
|
Even though not writing this extra code simplifies the task of handling multiple events, it does not eliminate the task. To get a handle on this situation, take a look at some simple sets of events that occur when a user performs an action. |
|
|
|
|
|
|
|
|
Consider, for example, the Logon dialog box that appears when you start Windows. If you imagine that this dialog box had been written in Visual Basic, as you enter your username and password, several events occur. |
|
|
|
|
|
|
|
|
First, look at the simple keystroke. For each character that you type in your username, three events are fired: KeyDown, KeyPress, and KeyUp (in that order). When you move from the username field to the password field, you change the focus from one control to another, so two additional events are triggered: LostFocus for the current control and GotFocus for the new control. |
|
|
|
|
|
|
|
|
Finally, as you click the OK button, the simple mouse click fires three events: MouseDown, MouseUp, and Click. If you double-click another object such as the form, two more events occur after the Click event: the DblClick event and another MouseUp event. That's five events for what would seem like a single user action. In addition, if the mouse click causes the focus to move from one control to another, LostFocus and GotFocus events would be triggered, for a total of seven events. |
|
|
|
|
|
|
|
|
Another problem is that different actions to achieve the same purpose cause different event sequences. For example, consider the OK button on the Windows Logon dialog box. Did you know that you can either click it with the mouse or tab over to it and press the spacebar? Using the mouse triggers the MouseDown, MouseUp, and Click events. Using the spacebar triggers the KeyDown, KeyPress, KeyUp, and then the Click events. This process can be confusing, can't it? |
|
|
|
|
|
|
|
|
However, you can determine what events will occur in your program and in what order they will occur. |
|
|
|
|
|
|
|
|
Determining the Order of Events |
|
|
|
|
|
|
|
|
The key to understanding the purposes of these events is knowing when they occur. If this information is still a bit unclear, try the simple tutorial that follows. In the tutorial, you create a program to monitor form events and then trigger those events to determine the firing order. You also preview the type of code you might want to put in each of these event procedures. |
|
|
|
|
|
|
|
|
Creating a Sample Form Events Program |
|
|
|
|
|
|
|
|
Five special events occur to each form, and you can place code in the associated event procedure for any of them: |
|
|
|
|
|
|
|
|
Load.Occurs when the form is loaded into memory |
|
|
|
|
|
|
|
|
Activate. Occurs when the form is displayed initially or when the user returns to the form from another form |
|
|
|
|
|
|
|
|
Deactivate. Occurs when the user moves to another form or the form is hidden |
|
|
|
|
|
|
|
|
Unload. Occurs when the form is unloaded from memory |
|
|
|
|
|
|
|
|
Initialize. Occurs when an instance of the form object is created |
|
|
|
|
|
|
|
|
Terminate. Occurs when an instance of the form object is destroyed |
|
|
|
|
|